/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/game_state_basic.h"
#include <stddef.h>


EXTERN_C_BEGIN


void
game_state_basic_destructor(game_state_basic_t* a_state __attribute__((unused)))
{
  /* Nothing, but may be useful in the future */
}

void
game_state_basic_init_first_time_only(game_state_basic_t* a_state)
{
  a_state->quit  = false;
  a_state->pause = false;
  a_state->update_speed = 1;
}


bool
game_state_basic_is_in_pause(game_state_basic_t* a_state)
{
  return a_state->pause;
}

void
game_state_basic_set_pause(game_state_basic_t* a_state, bool value)
{
  if(a_state != NULL)
    {
      a_state->pause = value;
    }
}

void
game_state_basic_toogle_pause(game_state_basic_t* a_state)
{
  if(a_state != NULL)
    {
      a_state->pause = ! a_state->pause;
    }
}


unsigned short
game_state_basic_get_update_speed(game_state_basic_t* a_state)
{
  return a_state == NULL ? 0 : a_state->update_speed;
}

bool
game_state_basic_reduce_update_speed(game_state_basic_t* a_state,
				     unsigned short reduction)
{
  if(a_state == NULL)
    {
      return false;
    }
  
  if(a_state->update_speed > reduction)
    {
      a_state->update_speed -= reduction;
      return true;
    }
  
  a_state->update_speed = 1;
  return false;
}

bool
game_state_basic_decrement_update_speed(game_state_basic_t* a_state)
{
  if(a_state == NULL || a_state->update_speed < 2)
    {
      return false;
    }
  
  --a_state->update_speed;
  return true;
}

bool
game_state_basic_increase_update_speed(game_state_basic_t* a_state,
				       unsigned short increase)
{
  if(a_state == NULL)
    {
      return false;
    }
  a_state->update_speed += increase;
  return true;
}

bool
game_state_basic_increment_update_speed(game_state_basic_t* a_state)
{
  if(a_state == NULL)
    {
      return false;
    }
  ++a_state->update_speed;
  return true;
}


bool
game_state_basic_has_to_quit(game_state_basic_t* a_state)
{
  return a_state->quit;
}

void
game_state_basic_set_quit(game_state_basic_t* a_state, bool value)
{
  if(a_state != NULL)
    {
      a_state->quit = value;
    }
}


#define STRUCT_PROPRIETY_EQUALS(struct1, struct2, propriety) \
  ( (struct1).propriety == (struct2).propriety )

bool
game_state_basic_equals(const game_state_basic_t* state1,
			const game_state_basic_t* state2)
{
  return
    state1 == state2 ||
    (state1 != NULL &&
     state2 != NULL &&
     STRUCT_PROPRIETY_EQUALS(*state1, *state2, quit)  &&
     STRUCT_PROPRIETY_EQUALS(*state1, *state2, pause) &&
     STRUCT_PROPRIETY_EQUALS(*state1, *state2, update_speed));
}


EXTERN_C_END
